home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / TextItem.C < prev    next >
C/C++ Source or Header  |  1992-07-06  |  2KB  |  119 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "TextItem.h"
  6.  
  7. #include "Class.h"
  8. #include "String.h"
  9. #include "Look.h"
  10.  
  11. //---- TextItem ----------------------------------------------------------------
  12.  
  13. NewMetaImpl(TextItem,VObject, (TP(text), TP(font), T(border)));
  14.  
  15. TextItem::TextItem(char *t, Font *f, Point b)
  16. {
  17.     font= f;
  18.     border= b;
  19.     text= 0;
  20.     strreplace(&text, t);
  21.     SetFlag(eVObjVFixed);
  22. }
  23.  
  24. TextItem::TextItem(int id, char *t, Font *f, Point b) : VObject(id)
  25. {
  26.     font= f;
  27.     border= b;
  28.     text= 0;
  29.     strreplace(&text, t);
  30.     SetFlag(eVObjVFixed);
  31. }
  32.  
  33. TextItem::~TextItem()
  34. {
  35.     SafeDelete(text);
  36. }
  37.  
  38. void TextItem::SetString(char *t, bool redraw)
  39. {
  40.     strreplace(&text, t);
  41.     SetContentRect(Rectangle(GetOrigin(), GetMinSize().extent), redraw);
  42. }
  43.  
  44. void TextItem::SetFString(bool redraw, char *va_(fmt), ...)
  45. {
  46.     va_list ap;
  47.     va_start(ap,va_(fmt));
  48.     if (redraw) {
  49.     InvalidateRect(contentRect);
  50.     strfreplace(&text, va_(fmt), ap);
  51.     CalcExtent();
  52.     InvalidateRect(contentRect);      
  53.     } else
  54.     strfreplace(&text, va_(fmt), ap);
  55.     Changed();
  56.     va_end(ap);
  57. }
  58.  
  59. void TextItem::SetFont(Font *fp)
  60. {
  61.     font= fp;
  62. }
  63.  
  64. Metric TextItem::GetMinSize()
  65. {
  66.     return Metric(font->Width((byte*)text), font->Spacing(), font->Ascender()).Expand(border);
  67. }
  68.  
  69. void TextItem::SetExtent(Point e)
  70. {
  71.     // VObject::SetExtent(GetMinSize().extent);
  72.     VObject::SetExtent(e);
  73. }
  74.  
  75. char *TextItem::AsString()
  76. {
  77.     return text;
  78. }
  79.  
  80. void TextItem::Draw(Rectangle)
  81. {
  82.     if (text && *text) {
  83.     Point p= contentRect.origin+border;
  84.     p.y+= font->Ascender();
  85.     GrShowString(font, Enabled() ? gInkBlack : gLook->DisableInk(), p, (byte*)text);
  86.     }
  87. }
  88.  
  89. int TextItem::Compare(Object *op)
  90. {
  91.     return StrCmp((byte*)text, (byte*)Guard(op,TextItem)->text, -1, sortmap);
  92. }
  93.  
  94. bool TextItem::IsEqual(Object *op)
  95. {
  96.     if (op == 0 || ! op->IsKindOf(TextItem))
  97.     return FALSE;
  98.     byte *s1= (byte*)AsString(), *s2= (byte*)op->AsString();
  99.     if (s1 == 0 || s2 == 0)
  100.     return FALSE;
  101.     return StrCmp(s1, s2, -1, sortmap) == 0;
  102. }
  103.  
  104. OStream& TextItem::PrintOn (OStream &s)
  105. {
  106.     VObject::PrintOn(s);
  107.     s << font SP << border SP;
  108.     return s.PrintString(text);
  109. }
  110.  
  111. IStream& TextItem::ReadFrom(IStream &s)
  112. {
  113.     SafeDelete(text);
  114.     VObject::ReadFrom(s);
  115.     s >> font >> border;
  116.     return s.ReadString(&text);
  117. }
  118.  
  119.